package emergency.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.whu.zj.R;
import com.whu.zj.UIHandler;

import java.util.ArrayList;
import java.util.List;

import emergency.adapter.EmergencyTypeAdapter;
import emergency.dto.Alert;
import emergency.network.GetEmergencyType;
import inter.Delegate;

/**
 * 预警处理
 * Created by Administrator on 2016/3/10.
 */
public class EmergencyType extends UIHandler.BaseActivity {

    Button btn_back;
    ListView lv_emergency;
    List<Alert> alerts = new ArrayList<Alert>();

    EmergencyTypeAdapter emergencyAdapter = new EmergencyTypeAdapter();

    {
        emergencyAdapter.setAlerts(alerts);
    }


    Delegate net4emergency = new Delegate() {
        @Override
        public void onResponse(String response) {
            initDataByNet(response);
        }

        @Override
        public void onErrorResponse(VolleyError error) {

        }
    };

    GetEmergencyType getEmergencyDeal = new GetEmergencyType();

    {
        getEmergencyDeal.setDelegate(net4emergency);
    }


    private void initDataByNet(String response) {
        if (!response.startsWith("[")) return;
        List<Alert> alertsTmp = JSON.parseArray(response, Alert.class);
        alerts.clear();
        alerts.addAll(alertsTmp);
        emergencyAdapter.notifyDataSetChanged();
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == btn_back) {
                EmergencyType.this.finish();
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_emergencytype);
        initview();
        initdata();
    }

    // 初始化数据
    private void initdata() {
        getEmergencyDeal.get();
    }

    private void initview() {
        btn_back = (Button) findViewById(R.id.id_btn_back);
        btn_back.setOnClickListener(onClickListener);

        lv_emergency = (ListView) findViewById(R.id.id_lv_emergency);
        lv_emergency.setAdapter(emergencyAdapter);

        lv_emergency.setDividerHeight((int) getResources().getDimension(R.dimen.y8));

    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (getEmergencyDeal != null)
            getEmergencyDeal.cancel();
    }
}
